import { Component, OnInit } from '@angular/core';
import { BladeContainerBase } from "../../../../shared/blade/blade-container-base";
import { Setofkey } from "../../../../entity/setofkey.model";
import { SetofkeyService } from "../../../../service/setofkey.service";
import { Observable } from "rxjs/Observable";
import { Command } from "../../../../shared/toolbar.service";
import { DialogResult } from "../../../../shared/dialog.service";
import { Blade } from "../../../../shared/blade/blade.model";
import { BladeData } from "../../../../shared/blade/blade-data";
import { ApplicationService } from "../../../../service/application.service";
import { ModelParamService } from "../../../../service/model-param.service";
import { ModelParam } from "../../../../entity/model-Param";
import { ModelParamEditComponent } from "../model-param-edit/model-param-edit.component";
import { DeviceCategoryService } from "../../../../service/device-category.service";
import { DeviceCategory } from "../../../../entity/device-category.model";
@Component({
  selector: 'app-model-param-list',
  templateUrl: './model-param-list.component.html',
  styleUrls: ['./model-param-list.component.css']
})
export class ModelParamListComponent extends BladeContainerBase<ModelParam> {
  /**
   * 型号参数集合
   * 
   * @type {ModelParam[]}
   * @memberof ModelParamListComponent
   */
  modelParams: ModelParam[] = [];
  deviceCategorys: DeviceCategory[] = [];
  constructor(
    private modelParamService: ModelParamService,
    private deviceCategoryService: DeviceCategoryService
  ) {
    super();
  }

  ngOnInit() {
    this.commands = [
      new Command("add", "添加", "fa fa-plus", "setofkey:insert", blade => this.exeAdd(blade), blade => this.canExeAdd(blade)),
      new Command("edit", "编辑", "fa fa-edit", "setofkey:update", blade => this.exeEdit(blade), blade => this.canExeEdit(blade)),
      new Command("delete", "删除", "fa fa-trash-o", "setofkey:delete", blade => this.exeDelete(blade), blade => this.canDelete(blade)),
    ];
    this.deviceCategoryService.getDeviceCategorys()
      .then(res => {
        if (res) {
          this.deviceCategorys = [...res];
          if (this.modelParams && this.modelParams.length && !this.modelParams[0].deviceCategoryName) {
            this.getDeviceCategoryName();
          }

        }
      })
      .catch(err => {
        console.log(err);
      })
    super.ngOnInit();

  }
  /**
   * 初始化数据
   * 
   * @returns {(any | Observable<any> | Promise<any>)} 
   * @memberof TurboperCategoryListComponent
   */
  initData(): any | Observable<any> | Promise<any> {
    this.modelParamService.getModelParams()
      .then(res => {
        if (res) {
          this.modelParams = [...res];
          this.currentEntity = res[0];
          if (this.deviceCategorys && this.deviceCategorys.length) {
            this.getDeviceCategoryName();
          }
        }
      })
      .catch(err => {
        console.log(err);
      })
      .then(res => this.bladeData.isLoading = false);




  }
  getDeviceCategoryName() {
    this.modelParams.forEach(modelParam => {
      const index = this.deviceCategorys.findIndex(deviceCategory => deviceCategory.deviceCategoryId == modelParam.deviceCategoryId);
      if (index > -1) {
        modelParam.deviceCategoryName = this.deviceCategorys[index].deviceCategoryName;
      }
    })
  }
  /**
   * 增加
   * 
   * @param {Blade} blend 
   * @memberof TurboperCategoryListComponent
   */
  exeAdd(blend: Blade): void {
    let data: BladeData = new BladeData();
    data.id = "modelParamEdit";
    data.title = "新建型号参数";
    data.isClosingDisabled = false;

    let createKeyBlade = this.bladeService.createBlade(ModelParamEditComponent, data, true, null, this.deviceCategorys);
    this.bladeService.showBlade(createKeyBlade, this.currentBlade);
  }
  canExeAdd(blend: Blade): boolean {
    return true;
  }


  /**
   * 编辑
   * 
   * @param {Blade} blend 
   * @memberof TurboperCategoryListComponent
   */
  exeEdit(blend: Blade): void {
    if (this.currentEntity != undefined) {
      let data: BladeData = new BladeData();
      data.id = "modelParamEdit";
      data.title = this.currentEntity.modelParamName;

      let editKeyBlade = this.bladeService.createBlade(ModelParamEditComponent, data, false, this.currentEntity, this.deviceCategorys);
      this.currentBlade.bladeService.showBlade(editKeyBlade, this.currentBlade);
    }
  }
  canExeEdit(blend?: Blade): boolean {
    return this.currentEntity != null;
  }


  /**
   * 删除
   * 
   * @param {Blade} blend 
   * @memberof TurboperCategoryListComponent
   */
  exeDelete(blend: Blade): void {
    let selectedKeys = this.getSelected();
    if (selectedKeys.length > 0) {
      this.dialogService
        .showConfirmationDialog("确认删除", "是否要删除选中项？")
        .subscribe(dialogResult => {
          if (dialogResult == DialogResult.Ok) {
            this.currentBlade.bladeData.isLoading = true;
            this.modelParamService.batchDelete(selectedKeys.map(o => o.paramId))
              .then(res => {
                if (res) {
                  return super.refresh(undefined);
                }

                this.dialogService
                  .showConfirmationDialog("删除失败", "");

              })
              .catch(error => this.bladeData.error = error)
              .then(res => this.bladeData.isLoading = false);
          }
        })
    }

  }
  canDelete(blend?: Blade): boolean {
    return this.getSelected().length > 0;
  }


  /**
   * 单选
   * 
   * @param {HTMLInputElement} event 
   * @param {ModelParam} key 
   * @memberof TurboperCategoryListComponent
   */
  onItemCheack(event: HTMLInputElement, key: ModelParam) {
    key.isChecked = event.checked;
  }

  /**
   * 全选
   * 
   * @param {HTMLInputElement} event 
   * @memberof TurboperCategoryListComponent
   */
  onAllCheack(event: HTMLInputElement) {
    if (this.modelParams.length > 0) {
      for (let i = 0; i < this.modelParams.length; i++) {
        this.modelParams[i].isChecked = event.checked;
      }
    }
  }
  onSeletedChange(modelParam: ModelParam) {
    if (this.currentBlade.childrenCanDeactivate()) {
      this.currentEntity = modelParam;
    }
  }


  /**
   * 获得选中项
   * 
   * @returns {TurboperCategory[]} 
   * @memberof TurboperCategoryListComponent
   */
  getSelected(): ModelParam[] {
    if (this.modelParams.length > 0) {
      let filter = this.modelParams.filter(ref => ref.isChecked)
      return filter == null ? [] : filter;
    }
    return [];
  }
}



// WEBPACK FOOTER //
// ./src/app/module/basic/model-param/model-param-list/model-param-list.component.ts